﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ProjectRoughWork
{
    class TestGameLogic : IGameLogic
    {
        /// <summary>
        /// Defeault Constructor for Test Game Logic. Initializers the actorMap, EventManager, ProcessorManager and the first id for the actorMap
        /// </summary>

        public TestGameLogic()
        {
            evtMng = EventManager.GetInstance();
            pcsMng = new ProcessManager();
            actorMap = new Dictionary<int, IActor>();
            lastActorID = 0;
        }

        /// <summary>
        /// Returns an instance of the GameLogic class, initialize the GameLogic class if necessary
        /// </summary>
        /// <returns>Returns an instance of the GameLogic class</returns>

        public static IGameLogic GetInstance()
        {
            if (pInstance == null)
            {
                pInstance = new TestGameLogic();
            }

            return pInstance;
        }

        /// <summary>
        /// Adds an actor to the GameLogic's actor map
        /// </summary>
        /// <param name="newActor">The new actor to added to the actor map</param>
        /// <returns>Returns true if the insertion was successful, returns false if the insertion failed or the actor already exists</returns>

        public bool VAddActor(IActor newActor)
        {
            KeyValuePair<int, IActor> test;
            test = new KeyValuePair<int, IActor>(0, newActor);

            ///
            /// Xbox uses a Silverlight version of the .NET framework.
            /// List.(T).Exists does not exist in the silverlight version.
            /// List(T).Contains is the equivalent 
            /// (http://msdn.microsoft.com/en-us/library/bhkz42b3(v=vs.95).aspx
            ///

            if (!actorMap.ToList().Contains(test))
           // if ( !actorMap.ToList().Exists(delegate(KeyValuePair<int, IActor> test){return(test.Value == newActor);} ) )
            {
                actorMap.Add(lastActorID, newActor);
                newActor.setID(lastActorID);
                lastActorID++;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Removes an actor from the GameLogic's actor map
        /// </summary>
        /// <param name="actorID">The ID of the actor to be removed</param>
        /// <returns>Returns true if the removal was successful, Returns false if the removal failed</returns>

        public bool VRemoveActor(int actorID)
        {
            return actorMap.Remove(actorID);
        }

        /// <summary>
        /// Retrieves the actor connected to the given ID from the GameLogic's actor map 
        /// </summary>
        /// <param name="actorID">The ID of the actor to be returned</param>
        /// <returns>Returns the actor connected with the given actorID if they exists, Returns null otherwise</returns>

        public IActor VFindActor(int actorID)
        {
            if (actorMap.ContainsKey(actorID))
            {
                return actorMap[actorID];
            }
            return null;
        }

        /// <summary>
        /// Moves the actor connected to the given ID to the new position given
        /// </summary>
        /// <param name="actorID">The ID of the actor to be moved</param>
        /// <param name="newPos">The new position for the actor connected to the given ID</param>

        public virtual void MoveActor(int actorID, Vector3 newPos)
        {
            if (actorMap.ContainsKey(actorID))
            {
                actorMap[actorID].setPosition(newPos);
            }
        }

        /// <summary>
        /// Attaches a process to the GameLogic's process manager
        /// </summary>
        /// <param name="newProcess">The new process to be attached</param>

        public void AttachProcess(CProcess newProcess)
        {
            pcsMng.Attach(newProcess);
        }

        /// <summary>
        /// Updates the GameLogic
        /// </summary>

        public virtual void VOnUpdate()
        {
            pcsMng.Run();
            evtMng.VRun();
        }

        /// <summary>
        /// Renders all necessary graphics if necessary
        /// </summary>

        public virtual void VRender()
        {

        }
        //Data Members

        protected int lastActorID;
        protected IEventManager evtMng;
        protected ProcessManager pcsMng;
        protected Dictionary<int, IActor> actorMap;

        protected static IGameLogic pInstance;
    }
}
